const express = require("express")
const app = express()

// 全局中间件
app.use((req, res, next) => {
  console.log('全局')
  next()
})

// 定义中间件函数
const mw1 = (req, res, next) => {
  console.log(1)
  next()
}

const mw2 = (req, res, next) => {
  next()
  console.log(2)
}

// 加上局部生效的中间件 可以定义多个
app.get('/user', mw1, mw2, (req, res) => {
  res.send('<h1 style="color: red">有中间件</h1>')
})

app.get('/about', (req, res) => {
  res.send('<h1 style="color: red">没有中间件</h1>')
})

app.listen(8088, () => {
  console.log('running')
})
